//订单详情
//施少伟奇

/**
 * 主视图
 */
interface IOrderManagentDetailViewModel extends ng.IScope {
    /**
    * 订单编号
    */
    orderId: string;
    /**
    * 下单时间
    */
    placeOrderTime: string;
    /**
    * 紧急程度
    */
    urgencyDegree: string;
    /**
    * 计划专员
    */
    plannerName: string;
    /**
    * 商务专员
    */
    businessAffairsName: string;
    /**
    * 客服专员
    */
    customerServiceName: string;
    /**
    * 调度专员
    */
    dispatchOfficerId: string;
    /**
     * 调度专员下拉
     */
    dispatchOfficerList: EmployeeListItemResponse[];
    /**
     * 客户单位
     */
    clientName: string;
    /**
     * 发货计划人
     */
    consignorName: string;
    /**
     * 发货人号码
     */
    consignorPhone: string;
    /**
     * 发货内容
     */
    Content: string;
    /**
     * 所需车型
     */
    carType: string;
    /**
     * 所需车长
     */
    carLength: string;
    /**
     * 承运方式
     */
    carriageWay: string;
    /**
     * 装车效果
     */
    loadingResult: string;
    /**
     * 备注
     */
    carryOrderRemarks: string;
    /**
     * 调度专员禁止输入
     */
    dispatchOfficerDis: boolean;
    /**
    * 货物重量单位下拉
    */
    goodQuantityUnitDropDown: ValueListItemResponse[];
    /**
    * 所需车型下拉
    */
    carTypeDropDown: ValueListItemResponse[];
    /**
    * 所需车长下拉
    */
    carLengthDropDown: ValueListItemResponse[];
    /**
    * 承运方式下拉
    */
    carrierCategoryDropDown: ValueListItemResponse[];
    /**
     * 总单信息对象
     */
    OData: orderManagementResponse;
    /**
    * 总单列表数据对象
    */
    orderTotalList: Array<orderManagementResponse>;
    /**
    * 子单列表数据对象
    */
    orderChildrenList: Array<invoiceManagementOrderChild>;
    /**
     *总单详情对象
     */
    orderDetial: orderManagementResponse;
    /**
    * 子单详情对象
    */
    orderChil: invoiceManagementOrderChild;
    /**
    * 点击获取总单详情事件
    */
    getOrderDetial: (data: orderManagementResponse) => void;
    /**
    * 点击获取子单详情事件
    */
    EditChildren: (data: invoiceManagementOrderChild) => void;
    /**
    * APP竞价结果
    */
    orderAppBiddingList: Array<quotationResponse>;
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 附件备注
     */
    attachmentDetails: string;
    /**
     * 返回事件
     */
    backUrl:()=>void
}



class OrderManagentDetailController {
    uploaderList: AddCarAttachmentDetail[];

    baseImageUrl: string;
    /**
     *订单id
     */
    id: string;
    /**
     *名称
     */
    name: string;
    /**
     *订单状态
     */
    type: string;
    /**
     *承运商id
     */
    carrierId: string;

    constructor(private $scope: IOrderManagentDetailViewModel,
        private routeService: routeService,
        
        private locationService: ILocationService,
        private valueService: IValueService,
        private orderService: IorderService,
        private employeeService: IEmployeeService,
        private $state: angular.ui.IStateService,
        // private areaApiService: IAreaService,
        private $location: ng.ILocationService,
        private goodsTypeService: IGoodsTypeService,
        private goodsService: IGoodsService,
        private $http: ng.IHttpService,
        private $ngBootbox: BootboxService,
        private commonService:ICommonService,
        private ossUploaderService:ossUploaderService) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.uploaderList = [];
        this.init();
    }

    init = () => {
        this.id = this.$location.search().id;
        this.name = this.$location.search().name;
        this.$scope.backUrl=this.backUrl;
        
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.driverUploader= new OssFile(this.$scope,this.$scope.driverUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});

       

        this.loadValueDropDown();
        this.loadDispatchOfficerDropDown();

        //填充总单列表
        this.orderService.orderManagentDetail(this.id).then((res) => {
            this.$scope.orderTotalList = [res.data]
            this.$scope.OData = res.data;
            this.$scope.getOrderDetial = this.getOrderDetial;

            this.$scope.orderChildrenList= res.data.childList;
            this.$scope.EditChildren = this.EditChildren;


            //附件信息
            var attachments = res.data.orderAttachments;
            if (attachments.length > 0) {
                attachments.forEach((item, index) => {
                    // this.commonService.showFileItem(item, this.$scope.driverUploader);
                    this.$scope.driverUploader.addByAttach(item)
                    this.uploaderList.push(item);
                })
            }
           
        })

        //填充APP竞价结果
        this.orderService.quotation(this.id).then((res) => {
            this.$scope.orderAppBiddingList = res.data.data
        })


    }


    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.Dispatch.toString(), 0, -1).then(result => {
            this.$scope.dispatchOfficerList = result.data.data;
        });
    }

    /** 
     * 填充所有静态下拉事件(除专员已外)
     */
    loadValueDropDown(): void {
        this.$scope.goodQuantityUnitDropDown = this.valueService.getGoodQuantityUnitList().data
        this.$scope.carTypeDropDown = this.valueService.getGoodQuantityUnitList().data
        this.$scope.carLengthDropDown = this.valueService.getGoodQuantityUnitList().data
        this.$scope.carrierCategoryDropDown = this.valueService.getGoodQuantityUnitList().data
    }

    /**
    * 点击获取总单详情事件
    */
    getOrderDetial = (data: orderManagementResponse)=> {
        this.$scope.orderDetial = data;
    }

    /**
    * 点击获取子单详情事件
    */
    EditChildren = (data: invoiceManagementOrderChild)=> {
        this.$scope.orderChil = data;
    }

    /**
     * 过滤图片格式
     */
    // ownFilter(file: File): boolean {
    //     let self:any = this;
    //     return self.queue.length < 10;
    // }

    /**
     * 选择上传文件后
     */
    onSuccessItem(fileName: string, filePath: string, tag: number): void {
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 删除上传的附件
     */
    onDelete(item: any): void {
        $("input[type=file]").val("");
        var imgName = item._file.name;
        this.uploaderList.forEach((item, index) => {
            if (item.attachmentName == imgName) {
                this.uploaderList.splice(index, 1);
            }
        });
    }




     /**
     * 返回事件
     */
    backUrl=()=>{
        this.$state.go('app.order.orderManagentController',{ name:'Edit'});
    }

}

angular.module("tms").controller("orderManagentDetailController", ['$scope', 'routeService',
    
    'locationService',
    'valueService',
    'orderService',
    'employeeService',
    '$state',
    '$location',
    'goodsTypeService',
    'goodsService',
    '$http',
    '$ngBootbox','commonService','ossUploaderService',
    OrderManagentDetailController]);